<template>
  <!-- 添加楼盘 value-->
  <div style="margin-top: 50px">
    <el-form :model="value" :rules="rules" ref="productInfoForm" label-width="120px" class="form-inner-container" size="small">
      <el-form-item label="楼盘等级：" prop="size">
        <!-- <el-cascader
          v-model="selectProductCateValue"
          :options="productCateOptions">
        </el-cascader> -->
        <el-select v-model="value.size" placeholder="请选择等级" clearable>
          <el-option
            v-for="item in brandOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      
      <el-form-item label="楼盘名称：" prop="name">
        <el-input v-model="value.name"></el-input>
      </el-form-item>
      <el-form-item label="楼盘地址：" prop="address">
        <el-input v-model="value.address"></el-input>
      </el-form-item>
      <el-form-item label="租金价格（每平米/天）：" >
        <el-input v-model="value.Rent"></el-input>
      </el-form-item>
      <el-form-item label="楼盘价格：">
        <p>5s /梯/周:<el-input
        
          v-model="value.price5"
       
          placeholder="请输入5s价格"></el-input></p>
          <p>10s /梯/周:<el-input
         
            v-model="value.price10"
         
            placeholder="请输入10s价格"></el-input></p>
            <p>15s /梯/周:<el-input
              
              v-model="value.price"
             
              placeholder="请输入15s价格"></el-input></p>
      </el-form-item>
      <el-form-item label="楼盘经度位置：">
        <el-input v-model="value.longitude"></el-input>
      </el-form-item>
      <el-form-item label="楼盘纬度位置：">
        <el-input v-model="value.latitude"></el-input>
      </el-form-item>
      <!-- <el-form-item label="楼盘电梯：">
      <p>实现默认必须填写一个 可以动态增加删除 且上传电梯图片</p>
      </el-form-item> -->
      <el-form-item label="入住规模：">
        <el-input v-model="value.Cheekin"></el-input>
      </el-form-item>
      <el-form-item label="禁止投放类别：">
        <el-input v-model="value.noType"></el-input>
      </el-form-item>
      <el-form-item label="入住率（%）：">
        <el-input v-model="value.CheekinPercent"></el-input>
      </el-form-item>
      <el-form-item label="最高楼层数：">
        <el-input v-model="value.tall" style="width: 300px"></el-input>
        <span style="margin-left: 20px">层</span>
      </el-form-item>
  

      <!-- <el-form-item label="头像" prop="avatarUrl">
        <ImageUpload v-model="aeDialogForm.avatarUrl" @change="getAvatarUrl" />
      </el-form-item> -->

      <el-form-item label="楼盘电梯数">
        <el-input v-model="value.lift"></el-input>
      </el-form-item>
      <el-form-item style="text-align: center">
        <el-button type="primary" size="medium" @click="handleNext('productInfoForm')">下一步，上传楼盘图片</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import {fetchListWithChildren} from '@/api/productCate'
  import {fetchList as fetchBrandList} from '@/api/brand'
  import {getProduct} from '@/api/product';
  export default {
    name: "ProductInfoDetail",

    props: {
      value: Object,
      isEdit: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        // 楼盘等级
        brandOptions: [{label:'TOP',value:1},{label:'5A',value:2},{label:'4A',value:3},{label:'3A',value:4},{label:'T-ap',value:5},{label:'H-ap',value:6}],//考虑是否新建等级表接口获取 目前写死

        hasEditCreated:false,
        //选中商品分类的值
        selectProductCateValue: [],
        productCateOptions: [],
        // brandOptions: [],
        rules: {
          name: [
            {required: true, message: '请输入楼盘名称', trigger: 'blur'},
            {min: 2, max: 140, message: '长度在 2 到 140 个字符', trigger: 'blur'}
          ],
          subTitle: [{required: true, message: '请输入楼盘详细地址', trigger: 'blur'}],
          productCategoryId: [{required: true, message: '请选择商品分类', trigger: 'blur'}],
          size: [{required: true, message: '请选择楼盘等级', trigger: 'blur'}],
          description: [{required: true, message: '请输入商品介绍', trigger: 'blur'}],
          requiredProp: [{required: true, message: '该项为必填项', trigger: 'blur'}]
        }
      };
    },
    created() {
      // this.getProductCateList();
      // this.getBrandList();
    },
    mounted(){
// console.log('总参数',this.value);
    },
    computed:{
      //商品的编号
      productId(){
        return this.value.id;
      }
    },
    watch: {
      productId:function(newValue){
        if(!this.isEdit)return;
        if(this.hasEditCreated)return;
        if(newValue===undefined||newValue==null||newValue===0)return;
        this.handleEditCreated();
      },
      selectProductCateValue: function (newValue) {
        if (newValue != null && newValue.length === 2) {
          this.value.productCategoryId = newValue[1];
          this.value.productCategoryName= this.getCateNameById(this.value.productCategoryId);
        } else {
          this.value.productCategoryId = null;
          this.value.productCategoryName=null;
        }
      }
    },
    methods: {
      //处理编辑逻辑
      handleEditCreated(){
        if(this.value.productCategoryId!=null){
          this.selectProductCateValue.push(this.value.cateParentId);
          this.selectProductCateValue.push(this.value.productCategoryId);
        }
        this.hasEditCreated=true;
      },
      getProductCateList() {
        fetchListWithChildren().then(response => {
          let list = response.data;
          this.productCateOptions = [];
          for (let i = 0; i < list.length; i++) {
            let children = [];
            if (list[i].children != null && list[i].children.length > 0) {
              for (let j = 0; j < list[i].children.length; j++) {
                children.push({label: list[i].children[j].name, value: list[i].children[j].id});
              }
            }
            this.productCateOptions.push({label: list[i].name, value: list[i].id, children: children});
          }
        });
      },
      getBrandList() {
        fetchBrandList({pageNum: 1, pageSize: 100}).then(response => {
          this.brandOptions = [];
          let brandList = response.data.list;
          for (let i = 0; i < brandList.length; i++) {
            this.brandOptions.push({label: brandList[i].name, value: brandList[i].id});
          }
        });
      },
      getCateNameById(id){
        let name=null;
        for(let i=0;i<this.productCateOptions.length;i++){
          for(let j=0;j<this.productCateOptions[i].children.length;j++){
            if(this.productCateOptions[i].children[j].value===id){
              name=this.productCateOptions[i].children[j].label;
              return name;
            }
          }
        }
        return name;
      },
      handleNext(formName){
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if(this.value.CheekinPercent>100){
            this.$message({
              message: '入住率不可高于100',
              type: 'error',
              duration:1000
            });
            return false
          }else if(this.value.CheekinPercent<=0){
            this.$message({
              message: '入住率不低于1',
              type: 'error',
              duration:1000
            });
            return false
          }else if(this.value.lift<1){
            this.$message({
              message: '电梯数不可小于1',
              type: 'error',
              duration:1000
            });
            return false
          }else {
            this.$emit('nextStep');

          }
          } else {
            this.$message({
              message: '请完善楼盘信息',
              type: 'error',
              duration:1000
            });
            return false;
          }
         
        });
      },
      handleBrandChange(val) {
        let brandName = '';
        for (let i = 0; i < this.brandOptions.length; i++) {
          if (this.brandOptions[i].value === val) {
            brandName = this.brandOptions[i].label;
            break;
          }
        }
        this.value.brandName = brandName;
      },
   
    }
  }
</script>

<style scoped>
</style>
